10. Cost 解决方案
我的 MSE
实现过程如下:
class MSE(Node):
def __init__(self, y, a):
"""
The mean squared error cost function.
Should be used as the last node for a network.
"""
# Call the base class' constructor.
Node.__init__(self, [y, a])
def forward(self):
"""
Calculates the mean squared error.
"""
# NOTE: We reshape these to avoid possible matrix/vector broadcast
# errors.
#
# For example, if we subtract an array of shape (3,) from an array of shape
# (3,1) we get an array of shape(3,3) as the result when we want
# an array of shape (3,1) instead.
#
# Making both arrays (3,1) insures the result is (3,1) and does
# an elementwise subtraction as expected.
y = self.inbound_nodes[0].value.reshape(-1, 1)
a = self.inbound_nodes[1].value.reshape(-1, 1)
m = self.inbound_nodes[0].value.shape[0]
diff = y - a
self.value = np.mean(diff**2)
MSE
反映的是等式 (5),其中 y 是目标输出,a 是神经网络计算的输出。然后将误差进行平方 (diff**2
),也可以写成 np.square(diff)
。最后,我们需要对平方误差求和并除以示例总数 m。可以通过 np.mean
或 (1 /m) * np.sum(diff**2)
进行计算。
注意 y
和 a
的顺序并不重要,(a - y
) 可以调换顺序,值还是相同的。